package hot100.滑动窗口;

import java.util.HashMap;
import java.util.Map;

/**
 * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长 子串 的长度。
 *
 * @ https://leetcode.cn/problems/longest-substring-without-repeating-characters/?envType=study-plan-v2&envId=top-100-liked
 * @date 2025/08/24
 */
public class 无重复字符的最长子串_3 {

    public static void main(String[] args) {

    }

    /**
     * 滑动窗口+哈希表
     */
    public int lengthOfLongestSubstring(String s) {
        int ans = 0;
        int left = 0;

        while (left < s.length()) {
            Map<Character, Integer> visited = new HashMap<>();
            int right = left;

            while (right < s.length() && !visited.containsKey(s.charAt(right))) {
                visited.put(s.charAt(right), right);
                right++;
            }
            ans = Math.max(ans, right - left);
            left = right == s.length() ? right : visited.get(s.charAt(right)) + 1;
        }
        return ans;
    }

}
